package com.lyy.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lyy.Vo.RoleMenu;
import com.lyy.Vo.RoleVo;
import com.lyy.aspect.ControllerEndpoint;
import com.lyy.response.Result;
import com.lyy.system.entity.Role;
import com.lyy.system.service.RoleService;
import com.sun.org.apache.regexp.internal.RE;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/role")
@Api(value = "角色管理模快",tags = "角色管理接口")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @PostMapping("/findRolePage")
    @ApiOperation(value = "分页查询角色列表",notes = "分页查询")
    public Result findRolePage(@RequestParam(required = true,defaultValue = "1") Integer current,
                               @RequestParam(required = true,defaultValue = "6") Integer size,
                               @RequestBody RoleVo roleVo){
        //对用户进行分页，泛型中注入的就是实体类
        Page<Role> page=new Page<>(current,size);
        //查询条件封装
        QueryWrapper<Role> wrapper=getWrapper(roleVo);
        IPage<Role> roleIPage= roleService.findRolePage(page,wrapper);
        Long total=roleIPage.getTotal();
        List<Role> roleList=roleIPage.getRecords();
        return Result.ok().data("total",total).data("roleList",roleList);
    }

    @ControllerEndpoint(exceptionMessage = "添加角色失败", operation = "添加角色")
    @PostMapping("/addRole")
    @ApiOperation(value = "添加角色",notes = "添加角色")
    public Result addRole(@RequestBody Role role) {
        roleService.addRole(role);
        return Result.ok();
    }

    @ControllerEndpoint(exceptionMessage = "修改角色失败", operation = "修改角色")
    @PostMapping("/updateRole")
    @ApiOperation(value = "修改角色",notes = "修改角色")
    public Result update(@RequestBody RoleVo roleVo) {
        roleService.update(roleVo);
        return Result.ok();
    }

    @ControllerEndpoint(exceptionMessage = "删除角色失败", operation = "删除角色")
    @DeleteMapping("/deleteRole")
    @ApiOperation(value = "删除角色",notes = "删除角色")
    public Result deleteById(Long id) {
        roleService.deleteById(id);
        return Result.ok();
    }

    @ControllerEndpoint(exceptionMessage = "角色授权失败", operation = "角色授权")
    @PostMapping("/menuAuth")
    @ApiOperation(value = "菜单授权",notes = "菜单授权")
    public Result menuAuth(@RequestBody List<RoleMenu> roleMenus) {
        System.out.println(roleMenus);
        roleService.menuAuth(roleMenus);
        return Result.ok();
    }

    @PostMapping("/roleDetail")
    @ApiOperation(value = "角色详情",notes = "角色详情")
    public Result roleDetail(Long id){
        Role role=roleService.roleDetail(id);
        return Result.ok().data("records",role);
    }

    @PostMapping("/getRoleByUserId")
    @ApiOperation(value = "根据用户Id获取角色",notes = "根据用户Id获取角色详情")
    public Result getRoleByUserId(Long id){
        QueryWrapper<Role> wrapper =new QueryWrapper<>();
        wrapper.eq("user_id",id);
        Role role=roleService.getRoleByUserId(wrapper);
        return Result.ok().data("roleDetailByUserId",role);
    }

    /**
     * 封装查询条件
     * @param roleVo
     * @return
     */
    private QueryWrapper<Role> getWrapper(RoleVo roleVo){
        QueryWrapper<Role> queryWrapper=new QueryWrapper<>();
        if (roleVo!=null){
            if (!StringUtils.isEmpty(roleVo.getId())){
                queryWrapper.eq("id",roleVo.getId());
            }
            if (!StringUtils.isEmpty(roleVo.getRoleName())){
                queryWrapper.eq("role_name",roleVo.getRoleName());
            }
            if (!StringUtils.isEmpty(roleVo.getRemark())){
                queryWrapper.eq("remark",roleVo.getRemark());
            }
        }
        queryWrapper.eq("status","1");
        return queryWrapper;
    }
}
